<?php

// term.php

require_once("overhead.php");

class Term {
	var$startDate;
	var$endDate;
	var$semester;
	var$ID;
	var $year;
	
	public function getID() {
		return $this->ID;
	}
	public function getStartDate() {
		return $this->startDate;
	}
	public function getEndDate() {
		return $this->endDate;
	}
	public function getSemester() {
		return $this->semester;
	}
	public function getYear() {
		return $this->year;
	}
	public function setStartDate($st) {
		$this->startDate=$st;
	}
	public function setEndDate($e) {
		$this->endDate=$e;
	}
	public function setSemester($se) {
		$this->semester=$se;
	}
	public function setYear($y) {
		$this->year=$y;
	}
	public function getStartMonth() {
		if ($this->semester=="Fall") {
			return "08";
		}
		if ($this->semester=="Spring") {
			return "02";
		}
		if ($this->semester=="Summer") {
			return "05";
		}
		return null;
	}
	public function setBasedOnYearMonth($ym) {
		$year = substr($ym,0,4);
		$sem = "";
		if (substr($ym,4) == "05") { $sem = "Summer"; }
		if (substr($ym,4) == "08") { $sem = "Fall"; }
		if (substr($ym,4) == "02") { $sem ="Spring"; }
		$this->semester=$sem;
		$this->year=$year;
	}
	
	public function retrieveFromID($i) { //Get Department info from the database for a department with Department.ID=$i
		$queryString="select ID, Semester, StartDate, EndDate,Year from Term where Term.ID=".$i;
		$query=mysql_query($queryString);
		$result=mysql_fetch_assoc($query);
		 if ($result) {
		 	//If something was found, put all the fields from the database (in $result right now) into
		 	//the instance variables for this object.3
 			$this->ID=$result['ID'];
 			$this->semester=$result['Semester'];
 			$this->startDate=$result['StartDate'];
			$this->endDate=$result['EndDate'];
			$this->year = $result['Year'];
 			//return true because we found it!
 			return true;
 		} else {
 			//it wasn't found or there was an error, so return false.
 			return false;
 		}
	}
	public function retrieveFromYearMonth($t) {
		$year = substr($t,0,4);
		if (substr($t,4) == "05") { $sem = "Summer"; }
		elseif (substr($t,4) == "08") { $sem = "Fall"; }
		elseif (substr($t,4) == "02") { $sem ="Spring"; }
		else { return false; }
		$queryString = mysql_query("select ID, Semester, StartDate, EndDate,Year from Term where Semester='".$sem."' and Year=".$year);
		$result=mysql_fetch_assoc($queryString);
		if ($result) {
		 	//If something was found, put all the fields from the database (in $result right now) into
		 	//the instance variables for this object.
 			$this->ID=$result['ID'];
 			$this->semester=$result['Semester'];
 			$this->startDate=$result['StartDate'];
			$this->endDate=$result['EndDate'];
			$this->year=$result['Year'];
 			//return true because we found it!
 			return true;
 		} else {
 			//it wasn't found or there was an error, so return false.
 			return false;
 		}	
	}
	
		public function update() { //Update the contents of the database to reflect any changes made to this object.
		$queryString="update Term set Semester='".$this->semester."', StartDate='".$this->startDate."', EndDate='".$this->endDate."' where Term.ID=".$this->ID;
		if (mysql_query($queryString)) { //if the query fails, then $query will be NULL or false. since we are not
										 //doing anything with any resulting rows, we don't have to create another variable
										 //for the result of mysql_query().
			return true; //Successfully updated!
		} else {
			return false; //There was a problem with the UPDATE query!
		}
	}
	
	public function insert() { //This takes all the values of the instance variables and creates a NEW entry in the 
							   //Department table for these values.
		$queryString="insert into Term (Semester,Year) values ('".$this->semester."',".$this->year.")";
		if (mysql_query($queryString)) { //if the insert was successful, this will not be null or false.
			//BUT! we have one more step. if you check the spefications, it says we have to update the value
			//of $this->ID to hold the ID that the database created (with auto_increment) for this new department.
			//We can do that by selecting LAST_INSERT_ID().
			$getIDQueryString="select LAST_INSERT_ID() as NewID";
			$getIDQuery=mysql_query($getIDQueryString);
			$result=mysql_fetch_assoc($getIDQuery);
			if ($result) {
				$this->ID=$result['NewID'];
				return true;
			} else { return false; }
		} else {
			return false;
		}
	}
	
	public function delete() { //This deletes the row of the Department table that has and ID corresponding
							   //to $this->ID
		$queryString="delete from Term where Term.ID=".$this->ID;
		if (mysql_query($queryString)) {
				return true;
		}
		else {
				return false;
		}
		
	}
	
	public function getLatest() { //Get Department info from the database for a department with Department.ID=$i
		$queryString="select ID, Semester, StartDate, EndDate,Year from Term where Term.ID=".$this->ID;
		$query=mysql_query($queryString);
		$result=mysql_fetch_assoc($query);
		 if ($result) {
		 	//If something was found, put all the fields from the database (in $result right now) into
		 	//the instance variables for this object.
 			$this->ID=$result['ID'];
 			$this->semester=$result['Semester'];
 			$this->startDate=$result['StartDate'];
			$this->endDate=$result['EndDate'];
			$this->year=$result['Year'];
 			//return true because we found it!
 			return true;
 		} else {
 			//it wasn't found or there was an error, so return false.
 			return false;
 		}
	}
	
}
?>